package com.zmj.sy.mom.srv.aps.service;

import com.zmj.sy.mom.srv.aps.bean.dto.staff.StaffPage;
import com.zmj.sy.mom.srv.aps.bean.dto.staff.StaffUserListReqDto;
import com.zmj.sy.mom.srv.aps.bean.dto.staff.StaffUserListResDto;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ShiftTeam;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Group;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdsReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.Result;
import com.zmj.sy.mom.srv.aps.bean.vo.shiftteam.ShiftTeamAddDetailReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.shiftteam.ShiftTeamAddReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.shiftteam.ShiftTeamAllResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.shiftteam.ShiftTeamListResVo;
import com.zmj.sy.mom.srv.aps.feign.StaffApiClient;
import com.zmj.sy.mom.srv.aps.mapper.GroupMapper;
import com.zmj.sy.mom.srv.aps.mapper.ShiftTeamMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class ShiftTeamService {

    private final ShiftTeamMapper shiftTeamMapper;

    private final GroupMapper groupMapper;

    private final StaffApiClient staffApiClient;

    @Transactional
    public void add(ShiftTeamAddReqVo reqVo) {
        Group g = groupMapper.selectById(reqVo.getGroupId());

        for (ShiftTeamAddDetailReqVo shiftTeamAddDetailReqVo : reqVo.getUserList()) {
            ShiftTeam shiftTeam = MkBeanUtils.copyProperties(shiftTeamAddDetailReqVo, ShiftTeam.class);
            shiftTeam.setGroupId(reqVo.getGroupId());
            shiftTeam.setGroupCode(g.getMyCode());
            shiftTeamMapper.insert(shiftTeam);
        }

    }

    @Transactional
    public void remove(BaseIdsReqVo reqVo) {
        for (Integer id : reqVo.getIds()) {
            shiftTeamMapper.deleteId(id);
        }
    }

    @Transactional(readOnly = true)
    public List<ShiftTeamListResVo> list(BaseIdReqVo reqVo) {
        List<ShiftTeam> shiftTeams = shiftTeamMapper.lambdaQuery()
                .eq(ShiftTeam::getGroupId, reqVo.getId())
                .list();

        return shiftTeams.stream()
                .map(e->{
                     return MkBeanUtils.copyProperties(e, ShiftTeamListResVo.class);
                })
                .collect(Collectors.toList());
    }

    @Transactional(readOnly = true)
    public List<ShiftTeamAllResVo> all(BaseIdReqVo reqVo) {

        StaffUserListReqDto r = new StaffUserListReqDto();
        r.setSize(9999);
        r.setPage(1);
        Result<StaffPage<StaffUserListResDto>> pageResult = staffApiClient.userList(r);

        List<ShiftTeam> shiftTeamList = shiftTeamMapper.lambdaQuery().eq(ShiftTeam::getGroupId, reqVo.getId()).list();

        Map<String, ShiftTeam> shiftTeamMap = shiftTeamList.stream().collect(Collectors.toMap(ShiftTeam::getUserCode, e -> e));

        return pageResult.getData()
                .getItems()
                .stream()
                .map(e -> {
                    ShiftTeam shiftTeam = shiftTeamMap.get(e.getStaffCode());

                    ShiftTeamAllResVo mapBean = new ShiftTeamAllResVo();
                    BeanUtils.copyProperties(e, mapBean);
                    if (shiftTeam != null) {
                        mapBean.setGroupId(shiftTeam.getGroupId());
                    }
                    mapBean.setUsername(e.getStaffName());
                    mapBean.setUserCode(e.getStaffCode());

                    return mapBean;
                })
                .collect(Collectors.toList());
    }
}
